﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using AKJ.Sharepoint.Branding.Config;
using System.Web.UI;

namespace AKJ.Sharepoint.Branding.Code
{
    public class BrandingManager
    {        
        /// <summary>
        /// Clear the cache, and insure we have a new fresh object
        /// </summary>
        public static void ClearBrandingCache()
        {
            HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Master.ToString()] = null;
            HttpContext.Current.Application[BrandingElementType.AKJ_Sharepoint_Css.ToString()] = null;
        }

       /// <summary>
        /// ....
        /// </summary>
        /// <param name="type"></param>
        /// <param name="priority"></param>
        /// <param name="value"></param>
        public static void InsertBrandingElement(BrandingElementType type, int priority, string value)
        {
            string key = type.ToString();
            
            if (string.IsNullOrEmpty(value)) return;                       
                     
            BrandingPair pair = new BrandingPair(priority, value);
                        
            // If no entry exist or the new one is larger use the new one
            if (HttpContext.Current.Application[key] == null || priority > (HttpContext.Current.Application[key] as BrandingPair).Priority)
            {
                HttpContext.Current.Application[key] = pair;
            }            
        }

        /// <summary>
        /// Returns the current Branding Element of the requested type (can be null).
        /// </summary>
        /// <param name="type"></param>
        /// <returns>Check for null</returns>
        public static BrandingPair GetCurrentBrandingElement(BrandingElementType type)
        {
            BrandingPair pair = HttpContext.Current.Application[type.ToString()] as BrandingPair;

            if (pair != null && pair is BrandingPair)
                return pair;
            else
                return null;
        }
    }
}
